package com.zzptc.myapp1125;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.zzptc.myapp1125.Service.CarService;
import com.zzptc.myapp1125.Service.CheliangpainpaiService;
import com.zzptc.myapp1125.entity.Car;
import com.zzptc.myapp1125.entity.Cheliangpinpai;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getCarInfo();
       getCheliangpinpai();
    }

    private void getCheliangpinpai() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://apis.juhe.cn")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CheliangpainpaiService cheliangpainpaiService=retrofit.create(CheliangpainpaiService.class);
        Call<Cheliangpinpai>call=cheliangpainpaiService.getAllpinpai("79729168b7661c89f1f4e56936121f31","A");
        call.enqueue(new Callback<Cheliangpinpai>() {
            @Override
            public void onResponse(Call<Cheliangpinpai> call, Response<Cheliangpinpai> response) {
                Cheliangpinpai cheliangpinpai=response.body();
                Log.d(TAG,"OnResponse:"+cheliangpinpai.getResult().get(0).getBrand_name());
            }

            @Override
            public void onFailure(Call<Cheliangpinpai> call, Throwable t) {
                Log.d(TAG,"onFailure:"+t.getMessage());

            }
        });

    }








    //
    private void getCarInfo() {
        //创建retrofit对象
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl("http://www.hnzzsgz.cn:5001/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        //创建接口对象
        CarService carService=retrofit.create(CarService.class);
        //调用接口中的抽象方法
        Call<Car>carCall=carService.getAllcar();
        //调用call类型中的异步方法
        carCall.enqueue(new Callback<Car>() {
            @Override
            public void onResponse(Call<Car> call, Response<Car> response) {
                Car carlist=response.body();//Car cars=response.boy(); 另一个方法
                //Log.d(TAG,"onResponse:"+carlist.getData().get(6).getCarno());另一个方法

                Log.d(TAG,"onResponse:"+new Gson().toJson(carlist));

                }


            @Override
            public void onFailure(Call<Car> call, Throwable t) {
                Log.d(TAG,"onFailure:"+t.getMessage());

            }
        });


    }
}
